package com.dgut.staffmodule.dao;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.dgut.staffmodule.entity.Department;

@Repository
public interface DepartmentRepository
		extends PagingAndSortingRepository<Department, Long>, JpaSpecificationExecutor<Department> {
	// 通过部门名称找到所属部门对象
	@Query(value = "select * from t_department where deptName = :deptName", nativeQuery = true)
	public Department findByDeptName(@Param("deptName") String deptName);

	// 新增员工后部门人数加1
	@Modifying
	@Query(value = "update t_department set count = count+1 where id =:id", nativeQuery = true)
	public int addCount(@Param("id") Long id);

	// 解雇员工后部门人数减1
	@Modifying
	@Query(value = "update t_department set count = count-1 where id =:id", nativeQuery = true)
	public int descCount(@Param("id") Long id);

	// 批量解雇员工后部门人数减去list.size()
	@Modifying
	@Query(value = "update t_department set count = count-:size where id =:id", nativeQuery = true)
	public int descCount(@Param("id") Long id, @Param("size") int size);

	// 查找部门领导
	@Query(value = "select * from t_department where id = :id", nativeQuery = true)
	public Department findDeptAudit(@Param("id") Long id);

}
